package com.software.lease.web.admin.service.impl;

import com.software.lease.common.constant.RedisConstant;
import com.software.lease.common.login.LoginUser;
import com.software.lease.common.login.LoginUserHolder;
import com.software.lease.common.result.ResultCodeEnum;
import com.software.lease.common.utils.JwtUtil;
import com.software.lease.model.entity.SystemUser;
import com.software.lease.model.enums.BaseStatus;
import com.software.lease.common.exception.LeaseException;
import com.software.lease.web.admin.mapper.SystemUserMapper;
import com.software.lease.web.admin.service.LoginService;
import com.software.lease.web.admin.vo.login.CaptchaVo;
import com.software.lease.web.admin.vo.login.LoginVo;
import com.software.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.wf.captcha.SpecCaptcha;
import jakarta.annotation.Resource;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SystemUserMapper systemUserMapper;

    /**
     * 生成验证码
     * @return
     */
    @Override
    public CaptchaVo getCaptcha() {
        // 三个参数分别为宽、高、位数
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        String code = specCaptcha.text().toLowerCase();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();

        //将uuid和验证码放入redis
        stringRedisTemplate.opsForValue().set(key,code,60, TimeUnit.SECONDS);

        //返回验证码
        return new CaptchaVo(specCaptcha.toBase64(),key);
    }

    /**
     * 登陆校验
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {
        //验证码为空
        if(loginVo.getCaptchaCode()==null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        String code = stringRedisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        //redis中的验证码过期
        if(code == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //验证码错误
        if(!code.equals(loginVo.getCaptchaCode().toLowerCase())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //查询用户信息
        SystemUser systemUser = systemUserMapper.selectByUsername(loginVo.getUsername());

        //未查询到相关用户
        if(systemUser == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }

        //账号被禁用
        if(systemUser.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //密码错误
        if(!systemUser.getPassword().equals(DigestUtils.md5Hex(loginVo.getPassword()))){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        //返回token
        return JwtUtil.createToken(systemUser.getId(),systemUser.getUsername());
    }

    /**
     * 查询用户信息
     * @return
     */
    @Override
    public SystemUserInfoVo info() {
        LoginUser loginUser = LoginUserHolder.getThreadLocal();
        String username = loginUser.getUsername();
        SystemUser systemUser = systemUserMapper.selectByUsername(username);

        return new SystemUserInfoVo(systemUser.getName(),systemUser.getAvatarUrl());
    }
}
